﻿using Colorful.Captcha;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Colorful.CMS.Core.Controllers
{
    public class CommonController : BaseController
    {
        private readonly IActionDescriptorCollectionProvider _provider;
        public CommonController(IActionDescriptorCollectionProvider provider)
        {
            _provider = provider;
        }

        #region 验证码
        [Route("~/Captcha")]
        public IActionResult GetCaptcha(string name)
        {
            //var cc = new CaptchaControl();
            //cc.IsForegroundDynamic = true;
            //cc.Height = 38;
            //if (!string.IsNullOrEmpty(name))
            //    cc.SessionName = name;
            //new ALSoft.Captcha.Core.Captcha().ProcessRequest(HttpContext);
            //return File(new byte[0], @"image/gif");
            return File(CaptchaHelper.GetCaptcha(), @"image/jpg");
        }
        #endregion

        #region 保持Session不丢失
        [Route("~/keepsession")]
        public IActionResult KeepSession()
        {
            return this.GetTextResult($"{DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")}");
        }
        #endregion

        #region 查看路由信息
        [Route("~/routesAjk31Mnoi90naPsd")]
        public IActionResult GetRoutes()
        {
            if (!WebService.IsDebug)
            {
                return this.GetTextResult("拒绝访问！");
            }
            var routes = _provider.ActionDescriptors.Items.Where(a => a.AttributeRouteInfo != null).Select(x => new
            {
                Url = x.AttributeRouteInfo.Template,
                Controller = x.RouteValues["Controller"],
                Action = x.RouteValues["Action"]

            }).OrderBy(a => a.Url).ToList();
            var html = new StringBuilder("<table class=\"am-table\" style=\"margin:30px\">");
            html.Append("<thead><tr><td>Url</td><td>Controller</td><td>Action</td></tr></thead>");
            html.Append("<tbody>");
            foreach (var item in routes)
            {
                html.Append($"<tr><td>{item.Url}</td><td>{item.Controller}</td><td>{item.Action}</td></tr>");
            }
            html.Append("</tbody></table>");
            return this.GetTextResult(html.ToString());
        }
        #endregion

        #region 登出
        [Route("~/logout")]
        public async Task<IActionResult> Logout()
        {
            var userid = this.HttpContext.GetUserId();
            if (!string.IsNullOrEmpty(userid))
                WebService.CacheService.Remove($"#{userid}");
            await this.HttpContext.SignOutAsync(IdentityService.AuthenticationScheme);
            HttpContext.Session.Clear();
            return this.GetTextResult("<h3>" + (this.Lang == "CN" ? "正在登出" : "Logout") + "...</h3><script>setTimeout(function(){window.top.location='/'},1500)</script>");
        }
        #endregion
    }
}
